package org.daochong.fastdev.funcs;

import java.sql.SQLException;

import org.daochong.fastdev.Context;
import org.daochong.fastdev.Function;

public class ConditionFunction implements Function {

	@Override
	public boolean is(String id) {
		return "if".equals(id) || "elseif".equals(id) || "endif".equals(id) || "else".equals(id);
	}

	@Override
	public void exec(String key, String prop, Context context) throws SQLException {
		if (!is(key) || prop == null || context == null)
			return;
		if (key.equals("if")) {
			Boolean cd = context.expressionCondition(prop);
			context.addCondition(cd);
		} else if (key.equals("elseif")) {
			if (!context.getConditionCurrent()) {
				context.setCondition(context.expressionCondition(prop));
			} else {
				context.setCondition(false);
			}
		} else if (key.equals("else")) {
			context.setCondition(context.getConditionElse());
		} else {
			context.endCondition();
		}
	}

}
